/**
 * Copyright (c) 2019 格创东智 All rights reserved.
 *
 * http://www.getech.cn
 *
 * 版权所有，侵权必究！
 */

package cn.getech.data.metadata.utils;

import cn.getech.data.metadata.annotation.ExplicitConstraint;
import cn.getech.data.metadata.model.excel.ExplicitInterface;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;


 /**
  * @description: map工具类
  * @author: wangzhaowen：kiss
  * @create: 2020/10/13 17:15
  * @version: 2020/10/13
  **/
public class MapUtils   {


    public static Map<String, Object> objectToMap(Object obj){
        if(obj == null){
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        Field[] superFields = obj.getClass().getSuperclass().getDeclaredFields();
        for (Field field : superFields) {
            field.setAccessible(true);
            try {
                map.put(field.getName(), field.get(obj));
            } catch (IllegalAccessException ignored) {
            }
        }
        Field[] declaredFields = obj.getClass().getDeclaredFields();
        for (Field field : declaredFields) {
            field.setAccessible(true);
            try {
                map.put(field.getName(), field.get(obj));
            } catch (IllegalAccessException ignored) {
            }
        }
        return map;
    }

     public static String getExceptionInfo(Exception e) {
         try {
             StringWriter sw = new StringWriter();
             PrintWriter pw = new PrintWriter(sw);
             e.printStackTrace(pw);
             sw.close();
             pw.close();
             return "\r\n" + sw.toString() + "\r\n";
         } catch (Exception e2) {
             return "";
         }
     }

     public static Map<Integer, String[]>  explicitSource(Class dataClass) {
         //循环获取对应列得下拉列表信息
         Map<Integer, String[]> explicitListConstraintMap = new HashMap<>();
         Field[] declaredFields = dataClass.getDeclaredFields();
         for (int i = 0; i < declaredFields.length; i++) {
             Field field = declaredFields[i];
             //解析注解信息
             ExplicitConstraint explicitConstraint = field.getAnnotation(ExplicitConstraint.class);
             if (explicitConstraint != null) {
                 String[] source = explicitConstraint.source();
                 if (source.length > 0) {
                     explicitListConstraintMap.put(i, source);
                 } else {
                     //动态下拉信息
                     Class<? extends ExplicitInterface>[] classes = explicitConstraint.sourceClass();
                     if (classes.length > 0) {
                         ExplicitInterface explicitInterface = null;
                         try {
                             explicitInterface = classes[0].newInstance();
                             String[] values = explicitInterface.source();
                             if (values != null && values.length > 0) {
                                 explicitListConstraintMap.put(i, values);
                             }
                         } catch (InstantiationException | IllegalAccessException ignored) {
                         }
                     }
                 }
             }
         }
         return explicitListConstraintMap;
     }

}
